home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / mus / edit / soundzapv3_0.lha / dunpack.c next >
C/C++ Source or Header  |  1992-06-05  |  1KB  |  36 lines

  1. /* DUnpack.c --- Fibonacci Delta decompression by Steve Hayes */
  2.  
  3. #include <exec/types.h>
  4.  
  5. /* Fibonacci delta encoding for sound data */
  6. BYTE codeToDelta[16] = {-34,-21,-13,-8,-5,-3,-2,-1,0,1,2,3,5,8,13,21};
  7.  
  8. /* Unpack Fibonacci-delta encoded data from n byte source
  9.  * buffer into 2*n byte dest buffer, given initial data
  10.  * value x.  It returns the lats data value x so you can
  11.  * call it several times to incrementally decompress the data.
  12.  */
  13.  
  14. BYTE D1Unpack(source,n,dest,x)
  15. BYTE *source, *dest;
  16. LONG n;
  17. BYTE x;
  18.    {
  19.    BYTE d;
  20.    LONG i, lim;
  21.  
  22.    lim = n << 1;
  23.    for (i=0; i < lim; ++i)
  24.       {
  25.       /* Decode a data nibble, high nibble then low nibble */
  26.       d = source[i >> 1];    /* get a pair of nibbles */
  27.       if (i & 1)             /* select low or high nibble */
  28.          d &= 0xf;           /* mask to get the low nibble */
  29.       else
  30.          d >>= 4;            /* shift to get the high nibble */
  31.       x += codeToDelta[d];   /* add in the decoded delta */
  32.       dest[i] = x;           /* store a 1 byte sample */
  33.       }
  34.    return(x);
  35.    }
  36.